package com.zim.access.common.chat;

import java.util.UUID;

/**
 * 消息建造者模式，通过该模式快速构造合适的消息
 *
 * @author hanyue
 * @version 1.0
 * @date 2021/12/21 上午12:37
 */
public class MessageBuilder<T> {

    private Message<T> message;

    public MessageBuilder() {
        this.message = new Message<T>();
        this.message.setId(UUID.randomUUID().toString());
        this.message.setSeqId(0L);
        this.message.setTimestamp(System.currentTimeMillis());
    }

    public MessageBuilder(String id) {
        this.message = new Message<T>();
        this.message.setId(id);
        this.message.setSeqId(0L);
        this.message.setTimestamp(System.currentTimeMillis());
    }

    public MessageBuilder(String id, Long seqId) {
        this.message = new Message<T>();
        this.message.setId(id);
        this.message.setSeqId(seqId);
        this.message.setTimestamp(System.currentTimeMillis());
    }

    public MessageBuilder setType(Message.Type type) {
        this.message.setType(type);
        return this;
    }

    public MessageBuilder setContent(T content) {
        this.message.setContent(content);
        return this;
    }

    public MessageBuilder setChatType(Message.ChatType chatType) {
        this.message.setChat(new Message.Chat().setType(chatType));
        return this;
    }

    public MessageBuilder setTo(String id) {
        this.message.setTo(new Message.To().setId(id).setTimestamp(getTimestamp()));
        return this;
    }

    public MessageBuilder setTo(Message.To to) {
        this.message.setTo(to);
        return this;
    }

    public MessageBuilder setFrom(String id) {
        this.message.setFrom(new Message.From().setId(id).setTimestamp(getTimestamp()));
        return this;
    }

    public MessageBuilder setFrom(Message.From from) {
        this.message.setFrom(from);
        return this;
    }

    public MessageBuilder setAck() {
        this.message.setAck(new Message.Ack().setTimestamp(getTimestamp()));
        return this;
    }

    public MessageBuilder setAck(Long timestamp) {
        this.message.setAck(new Message.Ack().setTimestamp(timestamp));
        return this;
    }

    public MessageBuilder setRead() {
        this.message.setRead(new Message.Read().setTimestamp(getTimestamp()));
        return this;
    }

    public MessageBuilder setRead(Long timestamp) {
        this.message.setRead(new Message.Read().setTimestamp(timestamp));
        return this;
    }

    public Message builder() {
        return this.message;
    }

    private Long getTimestamp() {
        return System.currentTimeMillis();
    }
}
